package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("role")
public class RoleController {


    @Reference
    private RoleService roleService;

    /**
     * 添加角色
     * @param menusIds
     * @param role
     * @return
     */
    @RequestMapping("addRole")
    public Result addRole(@RequestParam("menusIds") Integer [] menusIds, @RequestBody Role role){

        try {
            roleService.addRole(menusIds,role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FALL);
        }

    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FALL);

        }
    }

    @RequestMapping("deleteRoleById")
    public Result deleteRoleById(@RequestParam("id")Integer id){
        try {
            roleService.deleteRoleById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FALL);
        }
    }

    /**
     * 编辑更新
     * @param menusIds
     * @param role
     * @return
     */
    @RequestMapping("editRole")
   public Result editRole(@RequestParam("menusIds")Integer [] menusIds, @RequestBody Role role){

        try {
            roleService.editRole(menusIds,role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FALL);
        }

    }

    /*
     * @Author: hyy
     * @Description:查询所有的角色数据
     * @Date: 22:04 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Role> list = roleService.findAll();
            return new Result(true,"查询角色成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

}
